package com.mxchip.livestarmobile.mobile.rest

import com.mxchip.livestarmobile.mobile.util.Utils
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit


object RestEndpoint {

    private val DEBUG = Utils.isDebug()

    val publicAPI by lazy {
        builder.build().create(PublicApi::class.java)
    }

    private val builder by lazy {
        Retrofit.Builder().client(httpClient)
                .baseUrl("https://livestar-api.huixiaobao.tech/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(LiveDataCallAdapterFactory())
    }

    private val httpClient by lazy {

        /*val ai: ApplicationInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA)
        val bundle = ai.metaData
        val HXB_ACCESS_ID = bundle.getString("HXB_ACCESS_ID")*/

        OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS) //允许失败重试
                .retryOnConnectionFailure(true)
                .addInterceptor(loggingInterceptor)
                .build()
    }

    private val loggingInterceptor by lazy {
        val logInterceptor = HttpLoggingInterceptor()
        if (DEBUG) {
            logInterceptor.level = HttpLoggingInterceptor.Level.BODY
        } else {
            logInterceptor.level = HttpLoggingInterceptor.Level.NONE
        }

        logInterceptor
    }
}